package vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Tuple;
import java.util.ArrayList;
import java.util.List;

public class MysqlVerticle extends AbstractVerticle {
  //第一步 声明路由
  Router router;
  //第一配置连接参数
  MySQLConnectOptions connectOptions=new MySQLConnectOptions()
    .setPort(3306)
    .setHost("127.0.0.1")
    .setDatabase("test")
    .setUser("root")
    .setPassword("root");
    //第二个配置连接池 Pool Options
  PoolOptions poolOptions=new PoolOptions()
      .setMaxSize(5);
  //第三个 Create the chlient pool
  MySQLPool clint;
  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    clint= MySQLPool.pool(vertx,connectOptions, poolOptions);
    //第二部 初始化router
    router =Router.router(vertx);
    //第三步 将Router与vertx HttpServer 绑定

    //第四步配置解析Url 改成get 只能接受get请求
    router.get("/").handler(
      req -> {
        req.response()
          //text/plain修改json格式application/json
          .putHeader("content-type", "application/json")
          .end("Hello from Vert.x!");
      });
    //数据库访问
    router.get("/test/list").handler(
      req -> {
        Integer page=Integer.valueOf(req.request().getParam("page"));
        clint.getConnection(arl->{
          if(arl.succeeded()){
            System.out.println("Connected");
            //Obtain our connection
            SqlConnection conn=arl.result();
            Integer offset=(page-1)*10;
            conn.preparedQuery("select id from user limit 10 offset ?")
              .execute(Tuple.of(offset),ar2->{
                if(ar2.succeeded()){
                  List list=new ArrayList<JsonObject>();
                  ar2.result().forEach(item->{
                    JsonObject entries = new JsonObject();
                    entries.put("id",entries.getValue("id"));
                    list.add(entries);
                  });
                  req.response()
                    //text/plain修改json格式application/json
                    .putHeader("content-type", "application/json")
                    .putHeader("charset","utf-8")
                    .end(list.toString());
                    conn.close();
                }else {
                  req.response()
                    //text/plain修改json格式application/json
                    .putHeader("content-type", "application/json")
                    .putHeader("charset","utf-8")
                    .end(ar2.cause().toString());
                }
              });
          }else {
            System.out.println("Could not connect:"+arl.cause().getMessage());
          }
        });
      });
    vertx.createHttpServer().requestHandler(router).listen(8888,http->{
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }
}
